/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 6:48.
 */
package com.hyyt.shopjsp.coupon.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.coupon.pojo.CustomerDiscountCoupon;
import com.hyyt.shopjsp.coupon.pojo.DiscountCoupon;
import com.hyyt.shopjsp.coupon.service.ICustomerDiscountCouponService;
import com.hyyt.shopjsp.coupon.service.IDiscountCouponService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券管理
 *
 * @author Administrator on 2017/11/1 0001.
 */
@Controller
@RequestMapping("/back/discountCoupon")
public class DiscountCouponController extends BaseController {

    private Logger log = Logger.getLogger(DiscountCouponController.class);
    private String basePath = "/back/shop/coupon/";

    @Resource
    private IDiscountCouponService discountCouponService;
    @Resource
    private ICustomerDiscountCouponService customerDiscountCouponService;

    /**
     * 进入平台管理优惠券列表页面
     *
     * @return 返回数据到指定的页面
     */
    @RequestMapping("gotoDiscountCouponPage")
    public String gotoDiscountCouponPage() {
        return basePath + "discountCouponList";
    }

    /**
     * 查询平台管理优惠券集合
     * @param discountCouponName 优惠券名称
     * @param beginTime 开始时间
     * @param expirationTime 到期时间
     * @param useStatus 启用状态
     * @param isPass 审核状态
     * @param response 结果集
     */
    @RequestMapping(value = "listDiscountCoupon", method = RequestMethod.POST)
    @ResponseBody
    public void listDiscountCoupon(String discountCouponName,String beginTime, String expirationTime, String useStatus, String isPass, Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(discountCouponName.trim())) {//是否检索开始时间
            param.put("discountCouponName", discountCouponName);
        }
        if (Utils.objectIsNotEmpty(beginTime)) {//是否检索开始时间
            param.put("beginTime", beginTime);
        }
        if (Utils.objectIsNotEmpty(expirationTime)) {//是否检索到期时间
            param.put("expirationTime", expirationTime);
        }
        if (Utils.objectIsNotEmpty(useStatus)) {//是否检索启用状态
            param.put("useStatus", useStatus);
        }
        if (Utils.objectIsNotEmpty(isPass)) {//是否检索审核状态
            param.put("isPass", isPass);
        }

        int totalRecordCount = discountCouponService.countDiscountCoupon(param);
        param.put("sortColumns", "discountCouponId desc");
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<DiscountCoupon> list = discountCouponService.queryDiscountCoupon(param, page.getPageRecordBeginIndex(), page.getPageSize());

        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list

        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据id获得优惠券对象
     * @param discountCouponId 优惠券ID
     * @return 返回优惠券对象
     */
    @RequestMapping(value = "getDiscountCouponObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getDiscountCouponObject(Integer discountCouponId) {
        if (Utils.objectIsNotEmpty(discountCouponId)) {
            DiscountCoupon discountCoupon = discountCouponService.loadDiscountCoupon(discountCouponId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(discountCoupon));
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 保存或者修改优惠券信息
     *
     * @param discountCoupon 优惠券对象
     * @return 返回优惠券对象
     */
    @RequestMapping(value = "saveOrUpdateDiscountCoupon", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateDiscountCoupon(DiscountCoupon discountCoupon) {
        if (Utils.objectIsNotEmpty(discountCoupon)) {
            if (Utils.objectIsEmpty(discountCoupon.getDiscountCouponId())){
                String discountCouponCode =Utils.getRandomCode();
                discountCoupon.setDiscountCouponCode(discountCouponCode);
                discountCoupon.setCreateTime(new Timestamp(System.currentTimeMillis()));
                discountCoupon.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                discountCoupon.setSurplus(discountCoupon.getDistributionCount());
                discountCoupon.setUseStatus(0);
                discountCoupon.setIsPass(1);
                discountCouponService.saveDiscountCoupon(discountCoupon);
            }
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id删除优惠券对象
     * @param discountCouponId 优惠券ID
     * @return 返回布尔值
     */
    @RequestMapping(value = "deleteDiscountCoupon", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteDiscountCoupon(Integer discountCouponId) {
        if (Utils.objectIsNotEmpty(discountCouponId)) {
            discountCouponService.deleteDiscountCoupon(discountCouponId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 审核优惠券
     * @param discountCouponId 优惠券ID
     * @param isPass 审核状态
     * @return 返回布尔值
     */
    @RequestMapping(value = "auditDiscountCoupon", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg auditDiscountCoupon(Integer discountCouponId,Integer isPass) {
        if (Utils.objectIsNotEmpty(discountCouponId)&&Utils.objectIsNotEmpty(isPass)) {
            DiscountCoupon discountCoupon=discountCouponService.loadDiscountCoupon(discountCouponId);
            discountCoupon.setIsPass(isPass);
            discountCoupon.setCheckTime(new Timestamp(System.currentTimeMillis()));
            BasicUsers basicUsers = getSessionUsers();
            discountCoupon.setVerifier(basicUsers.getUserName());
            discountCouponService.updateDiscountCoupon(discountCoupon);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 优惠券启用状态
     * @param discountCouponId 优惠券ID
     * @param useStatus 启用状态
     * @return 返回布尔值
     */
    @RequestMapping(value = "initiateMode", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg initiateMode(Integer discountCouponId,Integer useStatus) {
        if (Utils.objectIsNotEmpty(discountCouponId)&&Utils.objectIsNotEmpty(useStatus)) {
            DiscountCoupon discountCoupon=discountCouponService.loadDiscountCoupon(discountCouponId);
            discountCoupon.setUseStatus(useStatus);
            discountCouponService.updateDiscountCoupon(discountCoupon);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 优惠券统计
     * @return 返回布尔值
     */
    @RequestMapping(value = "checkCouponAmount", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg checkCouponAmount() {
        Map<String,Object> param = new HashMap<>();
        BigDecimal openedCouponAmount=customerDiscountCouponService.countOpenedCouponAmount();
        BigDecimal usedCouponAmount=customerDiscountCouponService.countUsedCouponAmount();
        param.put("openedCouponAmount",openedCouponAmount);
        param.put("usedCouponAmount",usedCouponAmount);
        return ResultMsg.create(true,null,FastJsonUtils.toJSONString(param));
    }

    /**
     * 手动赠送优惠券
     * @param discountCouponId 优惠券ID
     * @param customerId 会员ID
     * @return 返回布尔值
     */
    @RequestMapping(value = "saveSendDiscountCoupon", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveSendDiscountCoupon(Integer discountCouponId,Integer customerId) {
        if (Utils.objectIsNotEmpty(discountCouponId)&&Utils.objectIsNotEmpty(customerId)) {
            DiscountCoupon discountCoupon=discountCouponService.loadDiscountCoupon(discountCouponId);
            CustomerDiscountCoupon customerDiscountCoupon=new CustomerDiscountCoupon();
            customerDiscountCoupon.setDiscountCouponId(discountCouponId);//优惠券ID
            customerDiscountCoupon.setCustomerId(customerId);//会员ID
            customerDiscountCoupon.setDiscountCouponCode(discountCoupon.getDiscountCouponCode());//优惠券码
            customerDiscountCoupon.setDiscountCouponName(discountCoupon.getDiscountCouponName());//优惠券名称
            customerDiscountCoupon.setDiscountCouponAmount(discountCoupon.getDiscountCouponAmount());//优惠金额
            customerDiscountCoupon.setDiscountCouponLowAmount(discountCoupon.getDiscountCouponLowAmount());//优惠下限金额
            customerDiscountCoupon.setBeginTime(discountCoupon.getBeginTime());//开始时间
            customerDiscountCoupon.setExpirationTime(discountCoupon.getExpirationTime());//到期时间
            customerDiscountCoupon.setDiscountImage(discountCoupon.getDiscountImage());//优惠券图片
            customerDiscountCoupon.setUseStatus(0);//使用状态：0，未使用 1，已使用
            customerDiscountCoupon.setStatus(1);//优惠券状态 1：正常 2：过期 3：作废
            customerDiscountCoupon.setCreateTime(new Timestamp(System.currentTimeMillis()));//创建时间
            customerDiscountCoupon.setUpdateTime(new Timestamp(System.currentTimeMillis()));//修改时间
            customerDiscountCouponService.saveCustomerDiscountCoupon(customerDiscountCoupon);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }
}
